package mi.payment.controller;

import android.text.TextUtils;
import android.view.View;

import mi.payment.R;
import mi.payment.constants.BaseErrorCode;
import mi.payment.interfaces.ITaskListener;
import mi.payment.interfaces.ITaskListener.DefaultTaskListener;
import mi.payment.settings.MerManagerSettings;
import mi.payment.settings.MerchantSettingsOperation;
import mi.payment.settings.NetworkDataOperations;
import mi.payment.settings.NetworkParamSettings;
import mi.payment.ui.base.ParentActivity;
import mi.payment.ui.other.LoginActivity;
import sunmi.sunmiui.dialog.DialogCreater;
import sunmi.sunmiui.dialog.HintDialog;

/**
 * @author Created by longx on 2017/7/19.
 */

public abstract class BaseCheckerController extends BaseController {

    private HintDialog hintDialog;

    public BaseCheckerController(ParentActivity activity) {
        super(activity);
    }

    /**
     * 是否检查商户号,默认检查
     */
    protected boolean isCheckMerchantNum() {
        return true;
    }

    /**
     * 是否检查终端号,默认检查
     */
    protected boolean isCheckTerminalNum() {
        return true;
    }

    /**
     * 是否检查IP,默认检查
     */
    protected boolean isCheckIP() {
        return true;
    }

    /**
     * 是否检查端口,默认检查
     */
    protected boolean isCheckPort() {
        return true;
    }

    /**
     * 是否检查TPDU,默认检查
     */
    protected boolean isCheckTPDU() {
        return true;
    }

    /**
     * 签到是否显示倒计时
     *
     * @return
     */
    public boolean isSigninTimeout() {
        return false;
    }

    public boolean isComplete() {
        MerManagerSettings merchantSettings = MerchantSettingsOperation.getInstance().getMerManagerSettings();
        if (isCheckMerchantNum() && TextUtils.isEmpty(merchantSettings.getMerchantNum())) {
            return false;
        }
        if (isCheckTerminalNum() && TextUtils.isEmpty(merchantSettings.getTerminalNum())) {
            return false;
        }

        NetworkParamSettings networkSettings = NetworkDataOperations.getInstance().getNetworkParamSettings();
        if (isCheckIP() && TextUtils.isEmpty(networkSettings.getHostIpAddress())) {
            return false;
        }
        if (isCheckPort() && TextUtils.isEmpty(networkSettings.getHostPort())) {
            return false;
        }
        if (isCheckTPDU() && TextUtils.isEmpty(networkSettings.getTPdu())) {
            return false;
        }
        return true;
    }

    /**
     * 弹参数不完整的弹框
     */
    public void showParamsIncompleteDialog() {
        hintDialog = DialogCreater.createHintDialog(activity, R.drawable.img_warning,
                getString(R.string.cancel), getString(R.string.confirm),
                getString(R.string.parameter_no_complete_dialog_msg),
                new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        dismissDialog();
                    }

                }, new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        dismissDialog();
                        activity.openActivity(LoginActivity.class);
                    }

                }, false);
        hintDialog.show();
    }

    /**
     * 弹要签到的弹框
     */
    public void showNoCheckInDialog() {
        hintDialog = DialogCreater.createHintDialog(activity, R.drawable.img_warning,
                getString(R.string.cancel), getString(R.string.confirm),
                getString(R.string.no_checkin_dialog_msg),
                new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        dismissDialog();
                    }

                }, new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        dismissDialog();
                        executeCheckIn();
                    }

                }, false);
        hintDialog.show();
    }

    private void executeCheckIn() {
        checkIn(new DefaultTaskListener<String>() {

            @Override
            public void onTaskSuccess(String ret) {
                showToast(getString(R.string.sign_success));
            }

            @Override
            public void onTaskFailed(String ret) {
                if (!TextUtils.equals(ret, "N1") && !TextUtils.equals(ret, "N2") && !TextUtils.equals(ret, "N3") && !TextUtils.equals(ret, "N4"))
                    showToast(getString(R.string.sign_fails, BaseErrorCode.getCode(ret)));
            }

        });
    }

    /**
     * 签到
     */
    public void checkIn(ITaskListener<String> listener) {
        ParamsInitCtrl paramsInitCtrl = new ParamsInitCtrl(activity);
        paramsInitCtrl.paramsInit(listener);
    }

    private void dismissDialog() {
        if (hintDialog != null) {
            hintDialog.dismiss();
            hintDialog = null;
        }
    }

}
